package HelloAlgo.graph;

import java.io.*;
import java.util.*;

public class Main {
    private static final int MAX_NUM = 12;
    private static final int PRODUCT_LIMIT = factorial(12); // 12! = 479001600

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int T = Integer.parseInt(br.readLine());
        for (int a = 0; a < T; a++) {
            String[] parts = br.readLine().split(" ");
            List<Integer> nums = new ArrayList<>();
            for (int i = 0; i < MAX_NUM; i++) {
                int count = Integer.parseInt(parts[i]);
                for (int j = 0; j < count; j++) {
                    nums.add(i + 1);
                }
            }

            Collections.sort(nums, Collections.reverseOrder()); // 从大到小处理
            System.out.println(minSetsOptimized(nums));
        }
    }

    private static int minSetsOptimized(List<Integer> nums) {
        int depth = 0;
        while (true) {
            List<Long> groups = new ArrayList<>();
            if (dfs(nums, 0, depth, groups)) {
                return depth;
            }
            depth++;
        }
    }

    private static boolean dfs(List<Integer> nums, int idx, int maxDepth, List<Long> groups) {
        if (idx == nums.size()) return true;
        if (groups.size() > maxDepth) return false;

        int num = nums.get(idx);

        for (int i = 0; i < groups.size(); i++) {
            long product = groups.get(i) * num;
            if (product <= PRODUCT_LIMIT) {
                groups.set(i, product);
                if (dfs(nums, idx + 1, maxDepth, groups)) return true;
                groups.set(i, product / num);
            }
        }

        // 新建一组
        groups.add((long) num);
        if (dfs(nums, idx + 1, maxDepth, groups)) return true;
        groups.remove(groups.size() - 1);

        return false;
    }

    private static int factorial(int n) {
        int res = 1;
        for (int i = 1; i <= n; i++) {
            res *= i;
        }
        return res;
    }
}
